<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jalur_distribusi extends CI_Controller{

	function __construct()
	{
		parent::__construct();
		$this->load->model("Peternakan_model");
		$this->load->model('Home_model');
		$this->load->model('Jalur_model');
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE){
			redirect('auth');
		}else{
			$id_role = $this->session->userdata('id_role'); 
			$data = array(
				"main_view" => "jalur_distribusi/konten",
				"title"		=> "Jalur Distribusi",
				"nama_user"	=> "Admin",
				
				"hak_akses0"=> $this->Home_model->get_hak_akses($id_role,0),
				"hak_akses1"=> $this->Home_model->get_hak_akses($id_role,1),
				"hak_akses2"=> $this->Home_model->get_hak_akses($id_role,2),
				
				"jalur"		=> $this->Jalur_model->get_jalur()
			);
			//var_dump($data["jalur"]);
			$this->load->view('template', $data);
		}
	}
	
	function tambah_jalur(){
		/*if($this->input->post("induk")=="0"){
			echo "kosong";
		} else {
			echo "bukan kosong";
		}*/
		$jalur = array(
			"NAMA"			=> $this->input->post("nama"),
			"CATATAN"		=> $this->input->post("catatan"),
			"INDUK"			=> $this->input->post("induk")
		);
		
		$max = $this->Jalur_model->get_max();
		
		if($max){
			$max = $max->result_array();
			$max = (int)$max[0]["ID"];
			echo "max:".$max." dan induk".$jalur["INDUK"];
			if($max==(int)$jalur["INDUK"]){
				$insert = $this->Jalur_model->tambah_jalur($jalur);
				if($insert){
					redirect("/jalur_distribusi");
				} else {
					echo "gagal tambah data baru yg normal";
				}
			} else {
				$ganti = $this->Jalur_model->ganti_id($max,$jalur);
				if($ganti){
					redirect("/jalur_distribusi");
				} else {
					echo "gagal ganti id";
				}
			}
		} else {
			echo "gagal get max";
		}
	}
	
	function delete_jalur(){
		if($this->Jalur_model->delete_jalur($this->input->post("id"))){
			echo json_encode("true");
		} else {
			echo json_encode("false");
		}
	}

}